package com.yanyu.space.sys.controller;


import com.yanyu.space.common.core.bean.db.PageEntity;
import com.yanyu.space.common.core.bean.web.R;
import com.yanyu.space.sys.bean.po.Role;
import com.yanyu.space.sys.bean.po.User;
import com.yanyu.space.sys.bean.vo.role.RoleAddVo;
import com.yanyu.space.sys.bean.vo.role.RoleSearchVo;
import com.yanyu.space.sys.bean.vo.role.RoleUpdateVo;
import com.yanyu.space.sys.bean.vo.user.UserAllocatedRoleVo;
import com.yanyu.space.sys.bean.vo.user.UserUnallocatedRoleVo;
import com.yanyu.space.sys.service.IRoleService;
import com.yanyu.space.sys.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.Arrays;
import java.util.List;

/**
 * 系统角色 前端控制器
 *
 * @author yanyu
 */
@RestController
@RequestMapping("/sys/role")
@Api(tags = "系统角色")
@AllArgsConstructor
@Validated
public class RoleController {

    private IRoleService roleService;
    private IUserService userService;

    @GetMapping("/list")
    @ApiOperation(value = "查询角色信息")
    public R<PageEntity<Role>> listRole(RoleSearchVo vo) {
        PageEntity<Role> page = roleService.listPage(vo);
        return R.ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation(value = "根据id查询角色信息")
    public R<Role> listRole(@PathVariable("id") Long id) {
        return R.ok(roleService.getById(id));
    }

    @PostMapping
    @ApiOperation(value = "新增角色")
    public R add(@RequestBody RoleAddVo vo) {
        roleService.add(vo);
        return R.ok();
    }

    @PutMapping
    @ApiOperation(value = "更新角色")
    public R update(@RequestBody RoleUpdateVo vo) {
        roleService.update(vo);
        return R.ok();
    }

    @PutMapping("/dataScope")
    @ApiOperation(value = "修改角色数据权限")
    public R dataScope(@RequestBody RoleUpdateVo vo) {
        roleService.updateDataScope(vo);
        return R.ok();
    }

    @PutMapping("/changeStatus")
    @ApiOperation(value = "修改角色状态")
    public R changeStatus(@RequestBody RoleUpdateVo vo) {
        roleService.updateStatus(vo);
        return R.ok();
    }

    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除角色")
    public R delete(@PathVariable("ids")List<Long> ids) {
        roleService.delete(ids);
        return R.ok();
    }

    @GetMapping("/authUser/allocated")
    @ApiOperation(value = "查询角色已授权用户列表")
    public R<PageEntity<User>> authUserAllocated(UserAllocatedRoleVo vo) {
        return R.ok(userService.listAllocated(vo));
    }

    @GetMapping("/authUser/unallocated")
    @ApiOperation(value = "查询角色未授权用户列表")
    public R<PageEntity<User>> authUserunAllocated(UserUnallocatedRoleVo vo) {
        return R.ok(userService.listUnallocated(vo));
    }

    @PutMapping("/authUser/cancel")
    @ApiOperation(value = "取消单个用户授权角色")
    public R authUserCancel(@RequestParam("roleId") Long roleId, @RequestParam("userId") Long userId) {
        roleService.delAllocatedUser(roleId, Arrays.asList(userId));
        return R.ok();
    }

    @PutMapping("/authUser/cancelAll")
    @ApiOperation(value = "批量取消用户授权角色")
    public R authUserCancelAll(@RequestParam("roleId") Long roleId, @RequestParam("userIds") @NotEmpty(message = "{NotEmpty}") List<Long> userIds) {
        roleService.delAllocatedUser(roleId, userIds);
        return R.ok();
    }

    @PutMapping("/authUser/selectAll")
    @ApiOperation(value = "查询角色未授权用户列表")
    public R authUserunSelectAll(@RequestParam("roleId") Long roleId, @RequestParam("userIds") @NotEmpty(message = "{NotEmpty}") List<Long> userIds) {
        roleService.addAllocatedUser(roleId, userIds);
        return R.ok();
    }
}

